﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using BYFW.SFL;
using LDSP.Util;
using LDSP.Helper;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class MngAccidentCase : LDSP.BRL.BaseValidUserPage
    {
        protected string EmptySearchStr
        {
            get { return this.GetVSString("EmptySearchStr_AccidentCase"); }
            set { this.SetVS("EmptySearchStr_AccidentCase", value); }
        }
        private List<int> PDFIDs = new List<int>();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.BindGrid();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected int col_index = 1;
        protected int col_accidentCaseType = 2;
        protected int col_name = 3;
        protected int col_summary = 4;
        protected int col_result = 5;
        protected int col_createDate = 6;
        protected int col_comment = 7;
        protected int col_reference = 8;
        private int col_pdf = 9;
        private int col_op = 10;

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "name";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return true;
        }
        protected void InitData()
        {
            BindDDLPageSize();
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }

            EmptySearchStr = Constants.EmptySearch.Accident_Case;
            UIHelper.SetDefaultFilterCondition(this.txt_name, EmptySearchStr);

            this.btn_add.Visible = IsSysAdmin;
            this.btn_delete.Visible = IsSysAdmin;

            this.SetSpecialPageInfo();
            this.BindDDLAccidentCaseType();
        }
        protected void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pageSize);
            this.ddl_pageSize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void InitDataDynamicBefore()
        {
            this.grd_list.Columns[0].Visible = IsSysAdmin;
        }
        private void InitDataDynamicAfter()
        {
            UIHelper.SetDefaultFilterCondition(this.txt_name, EmptySearchStr, false);
        }
        protected void BindGrid()
        {
            //更新关键字表,以便智能提示.
            string keyword = this.txt_name.Text.Trim();
            if (EmptySearchStr.Equals(keyword))
            {
                keyword = "";
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                new SearchKeywords().IncreaseWordsCount(keyword);
            }

            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new AccidentCase().GetRecords(g_pageInfo.ConditionWhere, false), g_pageInfo.GetSortExpression());

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            SavePageSizeSetting();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }

        protected void BindDDLAccidentCaseType()
        {
            DataTable dt = new AccidentCaseType().GetAll();
            this.ddl_accidentCaseType.Items.Clear();
            this.ddl_accidentCaseType.Items.Add("--请选择案例类别--");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_accidentCaseType.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_accidentCaseType.SelectedIndex = 0;
            }
            catch { }

        }
        protected void GetCondition()
        {
            string cond = "";
            string nameCond = this.txt_name.Text.Trim();
            if (nameCond != ""
                && nameCond != EmptySearchStr)
            {
                string tempCond = this.AppendConditionMulti("", nameCond, "name", "result", "summary", "reference");
                PDFIDs = new List<int>();
                if (chb_pdf.Checked)
                {
                    nameCond = UIHelper.CombindBlank(nameCond);
                    nameCond = nameCond.Replace(" ", " and ");
                    tempCond = this.AppendCondition(tempCond, string.Format("contains(pdfcontent,'{0}')", nameCond), false);
                    PDFIDs = UIHelper.GetPDFFilterIDsForAccidentCase(nameCond);
                }
                cond = this.AppendCondition(cond, tempCond, true);
                this.SetHighlight(nameCond);
            }
            if (this.ddl_accidentCaseType.SelectedIndex > 0)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("typeID", EDataType.Int, this.ddl_accidentCaseType.SelectedValue, "=", true), true);
            }
            if (this.txt_createStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("createDate", EDataType.Date, this.txt_createStartDT.Text, ">=", true), true);
            }
            if (this.txt_createEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("createDate", EDataType.Date, this.txt_createEndDT.Text, "<=", true), true);
            }
            g_pageInfo.Condition = cond;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.JSRedirect("../Admin/AddAccidentCase.aspx", false);
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.DeleteCheckedInGrid<AccidentCase>(this.grd_list);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }


        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                AccidentCase ett = new AccidentCase();
                ett.GetProperties(id);
                if (e.CommandName == "updateItem")
                {
                    this.JSRedirect("../Admin/AddAccidentCase.aspx?id=" + id, false);
                }
                else if (e.CommandName == "deleteItem")
                {
                    ett.Delete();
                }
                else if (e.CommandName == "downloadItem")
                {
                    this.StartDownload(ett.ID, DocType.事故案例);
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    AccidentCase ett = new AccidentCase();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, col_index);

                    e.Row.Cells[col_accidentCaseType].Text = ett.AccidentCaseType.Name;
                    e.Row.Cells[col_createDate].Text = this.FormatDT(ett.CreateDate);

                    //获取快照。
                    e.Row.Cells[col_summary].Text = this.GetSnapshot(ett.Summary);
                    e.Row.Cells[col_result].Text = this.GetSnapshot(ett.Result);
                    e.Row.Cells[col_reference].Text = this.GetSnapshot(ett.Reference);

                    this.HighlightMulti(e.Row, col_name, col_result, col_summary, col_reference, col_comment);

                    e.Row.Cells[col_summary].Text = UIHelper.AppendLink(e.Row.Cells[col_summary].Text, LDSP.Helper.LongTextInstanceNames.AccidentCase_Summary, Server.UrlEncode(this.GetHighlight()), ett.ID);
                    e.Row.Cells[col_result].Text = UIHelper.AppendLink(e.Row.Cells[col_result].Text, LDSP.Helper.LongTextInstanceNames.AccidentCase_Result, Server.UrlEncode(this.GetHighlight()), ett.ID);
                    e.Row.Cells[col_reference].Text = UIHelper.AppendLink(e.Row.Cells[col_reference].Text, LDSP.Helper.LongTextInstanceNames.AccidentCase_Reference, Server.UrlEncode(this.GetHighlight()), ett.ID);

                    UIHelper.AppendEmptyLine(e.Row, col_reference);

                    if (PDFIDs.Contains(ett.ID))
                    {
                        e.Row.Cells[col_pdf].Text = "包含";
                    }

                    //如果是企业用户，隐藏"修改"和"删除"按钮
                    if (LoginAdmin == null)
                    {
                        WebControl lnk_Modify = (WebControl)e.Row.FindControl("LinkButton2");
                        WebControl lnk_Delete = (WebControl)e.Row.FindControl("LinkButton3");
                        lnk_Modify.Visible = false;
                        lnk_Delete.Visible = false;
                    }

                    //设置tooltip
                    e.Row.ToolTip = StringUtil.ConstructTitle(ett);

                    UIHelper.EnableShowEnter(e.Row, col_result, col_summary, col_reference);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pageSize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}